﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Data;
using System.IO;
using System.Configuration;
using System.Transactions;
using System.Data.Linq;
using System.ComponentModel;
using UtilityExtensions;


namespace BOL.CentroCosto
{
    public class GetRecepcionDocExcel
    {

        int _count;

        public int Count(int idObra,
            DateTime fechaInicio,
            DateTime fechaFin,
            int startRowIndex,
            int maximumRows,
            string sortExpression)
        {
            return _count;
        }


        private IQueryable<RecepcionDocExcel> ApplySelect_RecepcionDocListInfo()
        {
            
            var q = from a in DbUtil.Db.RecepcionDocs
                    select new RecepcionDocExcel
                    {
                        beneficiario = a.proveedor,
                        concepto = a.descripcion,
                        FR=a.numRecepcion,
                        numDoc=String.Format("{0}",a.numDocumento),
                              //recordando para los casos de RH
                        costoSol = (a.idmoneda == 3 ?                          //TOTAL
                                    a.totalDoc.Value :
                                    (a.totalDoc.Value == 0 ?
                                                        0 :
                                                        Math.Round((decimal)(a.totalDoc.Value * a.tipoCambio.Value),
                                                        2,
                                                        MidpointRounding.ToEven)))
                                   - (a.idmoneda == 3 ?                         //IGV
                                    a.igvDoc.Value :
                                    (a.igvDoc.Value == 0 ?
                                                        0 :
                                                        Math.Round((decimal)(a.igvDoc.Value * a.tipoCambio.Value),
                                                        2,
                                                        MidpointRounding.ToEven))),
                        IGVSol = a.idmoneda == 3 ? 
                                    a.igvDoc.Value :
                                    (a.igvDoc.Value == 0 ? 
                                                        0 : 
                                                        Math.Round((decimal)(a.igvDoc.Value * a.tipoCambio.Value),
                                                        2, 
                                                        MidpointRounding.ToEven)),
                        totalSol = a.idmoneda == 3 ?
                                    a.totalDoc.Value :
                                    (a.totalDoc.Value == 0 ?
                                                        0 :
                                                        Math.Round((decimal)(a.totalDoc.Value * a.tipoCambio.Value),
                                                        2,
                                                        MidpointRounding.ToEven)),
                         //neto=toalSol  -retension
                        netoSol = a.idTipoDocumento==3?((a.idmoneda == 3 ?
                                                    a.totalDoc.Value :
                                                    (a.totalDoc.Value == 0 ?
                                                                        0 :
                                                                        Math.Round((decimal)(a.totalDoc.Value * a.tipoCambio.Value),
                                                                        2,
                                                                        MidpointRounding.ToEven)))
                                                    - (a.idmoneda == 3 ?
                                                    a.renta4taDoc.Value :
                                                    (a.renta4taDoc.Value == 0 ?
                                                                        0 :
                                                                        Math.Round((decimal)(a.renta4taDoc.Value * a.tipoCambio.Value),
                                                                        2,
                                                                        MidpointRounding.ToEven)))):0,

                        reta4taSol=a.idmoneda == 3 ? 
                                    a.renta4taDoc.Value :
                                    (a.renta4taDoc.Value == 0 ? 
                                                        0 : 
                                                        Math.Round((decimal)(a.renta4taDoc.Value * a.tipoCambio.Value), 
                                                        2, 
                                                        MidpointRounding.ToEven)),


                           //--------------------------------------------
                        costoDolar = (a.idmoneda == 4 ?
                                                           a.totalDoc.Value :
                                                           (a.totalDoc.Value == 0 ?
                                                                               0 :
                                                                               Math.Round((decimal)(a.totalDoc.Value / a.tipoCambio.Value),
                                                                               2,
                                                                               MidpointRounding.ToEven)) )
                                      - (a.idmoneda == 4 ?                         //IGV
                                                                    a.igvDoc.Value :
                                                                    (a.igvDoc.Value == 0 ?
                                                                                        0 :
                                                                                Math.Round((decimal)(a.igvDoc.Value / a.tipoCambio.Value),
                                                                                2,
                                                                                MidpointRounding.ToEven))),

                        IGVDolar = a.idmoneda == 4 ?
                                    a.igvDoc.Value :
                                    (a.igvDoc.Value == 0 ?
                                                        0 :
                                                        Math.Round((decimal)(a.igvDoc.Value / a.tipoCambio.Value),
                                                        2,
                                                        MidpointRounding.ToEven)),




                        totalDolar = a.idmoneda == 4 ?
                                   a.totalDoc.Value :
                                   (a.totalDoc.Value == 0 ?
                                                       0 :
                                                       Math.Round((decimal)(a.totalDoc.Value / a.tipoCambio.Value),
                                                       2,
                                                       MidpointRounding.ToEven)),
                        //neto=toalSol  -retension
                        netoDolar = a.idTipoDocumento == 3 ? ((a.idmoneda == 4 ?
                                                    a.totalDoc.Value :
                                                    (a.totalDoc.Value == 0 ?
                                                                        0 :
                                                                        Math.Round((decimal)(a.totalDoc.Value / a.tipoCambio.Value),
                                                                        2,
                                                                        MidpointRounding.ToEven)))
                                                    - (a.idmoneda == 4 ?
                                                    a.renta4taDoc.Value :
                                                    (a.renta4taDoc.Value == 0 ?
                                                                        0 :
                                                                        Math.Round((decimal)(a.renta4taDoc.Value / a.tipoCambio.Value),
                                                                        2,
                                                                        MidpointRounding.ToEven)))) : 0,

                        reta4taDolar = a.idmoneda == 4 ?
                                    a.renta4taDoc.Value :
                                    (a.renta4taDoc.Value == 0 ?
                                                        0 :
                                                        Math.Round((decimal)(a.renta4taDoc.Value / a.tipoCambio.Value),
                                                        2,
                                                        MidpointRounding.ToEven)),


                        
                        


                        tipoCambio=a.tipoCambio.Value,
                        fechaVenc=a.fechaVencimientoDoc.Value.ToShortDateString(),
                        fecha=a.fechaRecep.Value.ToShortDateString(),
                        idObra=a.idObra.Value,
                        tipoDocumento=a.DocumentoTipo.pg_TIPODOC

                    };
            return q;
        }


        [DataObjectMethod(DataObjectMethodType.Select, false)]
        public IEnumerable<RecepcionDocExcel> Execute(int idObra)
        {

            var q = ApplySelect_RecepcionDocListInfo();
                q = q.Where(a => a.idObra == idObra );
            _count = q.Count();
            return q;



        }

    }
}
